package com.example.demo.controller;

import com.example.demo.common.ApplicationVariable;
import com.example.demo.common.PasswordUtils;
import com.example.demo.common.ResultAjax;
import com.example.demo.model.Userinfo;
import com.example.demo.model.vo.UserinfoVO;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

    @RestController
    @RequestMapping("/user")
    public class UserController {

        @Autowired
        private UserService userService;

    /**
     * 注册
     * @param userinfo
     * @return
     */
    @RequestMapping("/reg")
    public ResultAjax reg(Userinfo userinfo) {
       // 1.校验参数
        if (userinfo == null || !StringUtils.hasLength(userinfo.getUsername()) ||
        !StringUtils.hasLength(userinfo.getPassword())) {
            return ResultAjax.fail(-1,"异常");
        }
        // 实现密码加盐
        userinfo.setPassword(PasswordUtils.encrypt(userinfo.getPassword()));
        // 2.请求接口 service 进行添加接口
        int ret = userService.reg(userinfo);
        // 3.将结果返回给前端
        return ResultAjax.success(ret);
    }


    /**
     * 登录
     * @param userinfoVO
     * @return
     */
    @RequestMapping("/login")
    public ResultAjax login(UserinfoVO userinfoVO, HttpServletRequest request) {
        // 1.参数校验
        if (userinfoVO == null || !StringUtils.hasLength(userinfoVO.getUsername())
                || !StringUtils.hasLength(userinfoVO.getPassword())) {
            // 非法登录
            return ResultAjax.fail(-1,"非法登录!");
        }
        // 2.根据用户名查询对象，判断用户名是否错误
        Userinfo userinfo = userService.getUserByName(userinfoVO.getUsername());
        if (userinfo == null && userinfo.getId() == 0) {
            return ResultAjax.fail(-2,"账号或密码错误!");
        }
        // 3.使用对象中的密码和输入的密码进行对比，判断密码是否错误
        // 加盐解密
        if (!PasswordUtils.decrypt(userinfoVO.getPassword(),userinfo.getPassword())) {
            return ResultAjax.fail(-2,"账号或密码错误!");
        }
        // 4.成功后将对象存储到 session 中
        HttpSession session = request.getSession();
        session.setAttribute(ApplicationVariable.SESSION_USERINFO_KEY,userinfo);
        // 5.结果返回给用户
        return ResultAjax.success(1);
    }

        /**
         * 注销
         * @param request
         * @return
         */
         @RequestMapping("/logout")
         public ResultAjax logout(HttpServletRequest request) {
             HttpSession session = request.getSession(false);
             if (session != null &&
                     session.getAttribute(ApplicationVariable.SESSION_USERINFO_KEY)!= null) {
                 session.removeAttribute(ApplicationVariable.SESSION_USERINFO_KEY);
             }

             return ResultAjax.success(1);
         }
    }
